#include "cslepui.h"
#include "cslep.h"
#include "slepbase.h"
#include <QtGui>



void CSLEPUI::actLogisticR(){
	double c;
	optsWidget = new SlepOptionsWidget(Method_Logistic_R);

	int success = optsWidget->exec();
	if(success == QDialog::Accepted){
		OPTS *opts = optsWidget->getOPTS();
		slep_matrix* A = optsWidget->getInputA();
		slep_vector* y = optsWidget->getInputy();
		slep_vector* x = slepAllocDenseVector(A->n);
		slep_vector* funVal = slepAllocDenseVector(opts->maxIter);
		slep_vector* valueL = slepAllocDenseVector(opts->maxIter);

		LogisticR(A,y,optsWidget->getL1RegCoefficient(),opts,x,&c,funVal,valueL);

		SLEPItem *itemA = SLEPFileOperations::slepMatrix2SlepItem(A);
		SLEPItem *itemy = SLEPFileOperations::slepVector2SlepItem(y);
		SLEPItem *itemx = SLEPFileOperations::slepVector2SlepItem(x);
		SLEPItem *itemFVal = SLEPFileOperations::slepVector2SlepItem(funVal);

		{
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			SLEPItem itemC;
			itemC.singlevalue=c;
			itemC.type=SLEPItem::SINGLE_VALUE;
            itemModel->insertRow(0);
            slepItemList->insert(0,itemC);
            itemModel->setData(itemModel->index(0,0),"c");
            itemModel->setData(itemModel->index(0,1),"Scalar");
            itemModel->setData(itemModel->index(0,2),c);
			itemModel->setData(itemModel->index(0,3),"");
		}
        if(itemA->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("A");
			SLEPFileOperations::writeTextFile(*itemA,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemA);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemA->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}

        if(itemy->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("y");
			SLEPFileOperations::writeTextFile(*itemy,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemy);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemy->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}

        if(itemx->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("x");
			SLEPFileOperations::writeTextFile(*itemx,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemx);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemx->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}
        if(itemFVal->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("fVal");
			SLEPFileOperations::writeTextFile(*itemFVal,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemFVal);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemFVal->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}
		slepFreeMatrix(A);
		slepFreeVector(x);
		slepFreeVector(y);
		slepFreeVector(funVal);
		slepFreeVector(valueL);
		QMessageBox::information(this,tr("Success"), tr("It was run successfully!"));
	}
	else{
		QMessageBox::information(this,tr("Error"), tr("Was not able to execute as there were errors in input!"));
	}
	delete optsWidget;
}



void CSLEPUI::actnnLogisticR(){
	double c;
	optsWidget = new SlepOptionsWidget(Method_nnLogistic_R);

	int success = optsWidget->exec();
	if(success == QDialog::Accepted){
		OPTS *opts = optsWidget->getOPTS();
		slep_matrix* A = optsWidget->getInputA();
		slep_vector* y = optsWidget->getInputy();
		slep_vector* x = slepAllocDenseVector(A->n);
		slep_vector* funVal = slepAllocDenseVector(opts->maxIter);
		slep_vector* valueL = slepAllocDenseVector(opts->maxIter);

		nnLogisticR(A,y,optsWidget->getL1RegCoefficient(),opts,x,&c,funVal);

		SLEPItem *itemA = SLEPFileOperations::slepMatrix2SlepItem(A);
		SLEPItem *itemy = SLEPFileOperations::slepVector2SlepItem(y);
		SLEPItem *itemx = SLEPFileOperations::slepVector2SlepItem(x);
		SLEPItem *itemFVal = SLEPFileOperations::slepVector2SlepItem(funVal);

		{
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			SLEPItem itemC;
			itemC.singlevalue=c;
			itemC.type=SLEPItem::SINGLE_VALUE;
            itemModel->insertRow(0);
            slepItemList->insert(0,itemC);
            itemModel->setData(itemModel->index(0,0),"c");
            itemModel->setData(itemModel->index(0,1),"Scalar");
            itemModel->setData(itemModel->index(0,2),c);
			itemModel->setData(itemModel->index(0,3),"");
		}
        if(itemA->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("A");
			SLEPFileOperations::writeTextFile(*itemA,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemA);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemA->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}

        if(itemy->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("y");
			SLEPFileOperations::writeTextFile(*itemy,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemy);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemy->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}

        if(itemx->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("x");
			SLEPFileOperations::writeTextFile(*itemx,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemx);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemx->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}
        if(itemFVal->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("fVal");
			SLEPFileOperations::writeTextFile(*itemFVal,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemFVal);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemFVal->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}
		slepFreeMatrix(A);
		slepFreeVector(x);
		slepFreeVector(y);
		slepFreeVector(funVal);
		slepFreeVector(valueL);
		QMessageBox::information(this,tr("Success"), tr("It was run successfully!"));
	}
	else{
		QMessageBox::information(this,tr("Error"), tr("Was not able to execute as there were errors in input!"));
	}
	delete optsWidget;
}



void CSLEPUI::actLogisticC(){
	double c;
	optsWidget = new SlepOptionsWidget(Method_Logistic_C);

	int success = optsWidget->exec();
	if(success == QDialog::Accepted){
		OPTS *opts = optsWidget->getOPTS();
		slep_matrix* A = optsWidget->getInputA();
		slep_vector* y = optsWidget->getInputy();
		slep_vector* x = slepAllocDenseVector(A->n);
		slep_vector* funVal = slepAllocDenseVector(opts->maxIter);
		slep_vector* valueL = slepAllocDenseVector(opts->maxIter);

		LogisticC(A,y,optsWidget->getL1RegCoefficient(),opts,x,&c,funVal,valueL);

		SLEPItem *itemA = SLEPFileOperations::slepMatrix2SlepItem(A);
		SLEPItem *itemy = SLEPFileOperations::slepVector2SlepItem(y);
		SLEPItem *itemx = SLEPFileOperations::slepVector2SlepItem(x);
		SLEPItem *itemFVal = SLEPFileOperations::slepVector2SlepItem(funVal);

		{
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			SLEPItem itemC;
			itemC.singlevalue=c;
			itemC.type=SLEPItem::SINGLE_VALUE;
            itemModel->insertRow(0);
            slepItemList->insert(0,itemC);
            itemModel->setData(itemModel->index(0,0),"c");
            itemModel->setData(itemModel->index(0,1),"Scalar");
            itemModel->setData(itemModel->index(0,2),c);
			itemModel->setData(itemModel->index(0,3),"");
		}
        if(itemA->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("A");
			SLEPFileOperations::writeTextFile(*itemA,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemA);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemA->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}

        if(itemy->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("y");
			SLEPFileOperations::writeTextFile(*itemy,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemy);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemy->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}

        if(itemx->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("x");
			SLEPFileOperations::writeTextFile(*itemx,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemx);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemx->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}
        if(itemFVal->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("fVal");
			SLEPFileOperations::writeTextFile(*itemFVal,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemFVal);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemFVal->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}
		slepFreeMatrix(A);
		slepFreeVector(x);
		slepFreeVector(y);
		slepFreeVector(funVal);
		slepFreeVector(valueL);
		QMessageBox::information(this,tr("Success"), tr("It ran successfully!"));
	}
	else{
		QMessageBox::information(this,tr("Error"), tr("Was not able to execute as there were errors in input or you cancelled!"));
	}
	delete optsWidget;
}



void CSLEPUI::actnnLogisticC(){
	double c;
	optsWidget = new SlepOptionsWidget(Method_nnLogistic_C);

	int success = optsWidget->exec();
	if(success == QDialog::Accepted){
		OPTS *opts = optsWidget->getOPTS();
		slep_matrix* A = optsWidget->getInputA();
		slep_vector* y = optsWidget->getInputy();
		slep_vector* x = slepAllocDenseVector(A->n);
		slep_vector* funVal = slepAllocDenseVector(opts->maxIter);
		slep_vector* valueL = slepAllocDenseVector(opts->maxIter);

		nnLogisticC(A,y,optsWidget->getL1RegCoefficient(),opts,x,&c,funVal);

		SLEPItem *itemA = SLEPFileOperations::slepMatrix2SlepItem(A);
		SLEPItem *itemy = SLEPFileOperations::slepVector2SlepItem(y);
		SLEPItem *itemx = SLEPFileOperations::slepVector2SlepItem(x);
		SLEPItem *itemFVal = SLEPFileOperations::slepVector2SlepItem(funVal);

		{
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			SLEPItem itemC;
			itemC.singlevalue=c;
			itemC.type=SLEPItem::SINGLE_VALUE;
            itemModel->insertRow(0);
            slepItemList->insert(0,itemC);
            itemModel->setData(itemModel->index(0,0),"c");
            itemModel->setData(itemModel->index(0,1),"Scalar");
            itemModel->setData(itemModel->index(0,2),c);
			itemModel->setData(itemModel->index(0,3),"");
		}
        if(itemA->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("A");
			SLEPFileOperations::writeTextFile(*itemA,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemA);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemA->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}

        if(itemy->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("y");
			SLEPFileOperations::writeTextFile(*itemy,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemy);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemy->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}

        if(itemx->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("x");
			SLEPFileOperations::writeTextFile(*itemx,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemx);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemx->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}
        if(itemFVal->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("fVal");
			SLEPFileOperations::writeTextFile(*itemFVal,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemFVal);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemFVal->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}
		slepFreeMatrix(A);
		slepFreeVector(x);
		slepFreeVector(y);
		slepFreeVector(funVal);
		slepFreeVector(valueL);
		QMessageBox::information(this,tr("Success"), tr("It ran successfully!"));
	}
	else{
		QMessageBox::information(this,tr("Error"), tr("Was not able to execute as there were errors in input!"));
	}
	delete optsWidget;
}
